import { getOrderList, cancelOrder, finishOrder } from '../../utils/requestApi.js';
import { toPay } from '../../utils/util.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
    tabbar: ['全部订单', '待收款', '待发货', '待收货', '待评价'],
    activeItem: 0, // tabbar激活项
    list: [],  //数据列表
    showEmpty: false, //显示空的页面
    page: 1, //页码
    hiddenLoading: true, //加载动画
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ activeItem: options.type == 4 ? 0 : parseInt(options.type) + 1 })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ page: 1 })
    this.initData('tabbar');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  // 选择tabbar
  selectTabbar:function(e){
    this.setData({ activeItem: e.detail.index })
    this.initData();
  },

  //初始化页面数据
  initData:function(param){
    let { page, list } = this.data;
    if (param != 'onReachBottom') {
      list = []; 
      page = 1;
      wx.showLoading({ title: '加载中...' })
    }
    getOrderList({
      ShowGiftOrder: true, // 显示礼品订单
      IsDrainaging: 2,
      pageIndex: page,
      pageSize: 10,
      status: this.data.activeItem == 4 ? 21 : this.data.activeItem
    }).then(res =>{
      wx.hideLoading();
      this.setData({ hiddenLoading: true })
      if (res.data.Status == 'Success' && res.data.Data.length != 0){
        list = [...list, ...res.data.Data]
        page ++;
      } else this.setData({ showEmpty: true })
      this.setData({ list, page })
    })
  },
  
  //打开订单详情页面
  toOrderDetail:function(e){
    let { id } = e.currentTarget.dataset;
    let { list } = this.data;
    let curOrder = null;
    list.forEach(item =>{
      if (item.OrderId == id) curOrder = JSON.stringify(item);
    })
    wx.navigateTo({
      url: `../orderDetail/orderDetail?id=${id}`,
    })
  },
  
  //取消订单
  cancelOrd:function(e){
    let { list } = this.data;
    let { id } = e.currentTarget.dataset;
    wx.showModal({
      content: '确定取消订单？',
      success: (res) =>{
        if (res.confirm) {
          cancelOrder({
            orderId: id
          }).then(res => {
            if (res.data.Status == 'Success') this.initData();
            wx.showToast({ icon: 'none', title: res.data.Message })
          })
        }
      }
    })
  },
  
  //去支付
  pay:function(e){
    let { list } = this.data;
    let { id } = e.currentTarget.dataset;
    let _this = this;
    toPay(id, function(res){
      _this.initData();
    })
  },

  // 收货
  takeOver(e) {
    let { list } = this.data;
    let { id } = e.currentTarget.dataset;
    wx.showModal({
      content: '确认收货？',
      success: res=> {
        if (res.confirm) {
          finishOrder({ orderId: id }).then(res => {
            if (res.data.Status == 'Success') this.initData();
            wx.showToast({ icon: 'none', title: res.data.Message })
          })
        }
      }
    })
  },

  //点击评论
  onComment(e) {
    let { id } = e.currentTarget.dataset;
    this.data.list.forEach((item, index) => {
      if (item.OrderId == id) wx.navigateTo({ url: `/pages/goodsComment/goodsComment?list=${JSON.stringify(item.LineItems)}&orderId=${id}` })
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({ hiddenLoading: false })
    this.initData('onReachBottom');    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})